/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.amiee.nicetab.library;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

/* * 用于需要圆角矩形框背景的TextView的情况,减少直接使用TextView时引入的shape资源文件 */
public class MsgView extends Text {
    private int backgroundColor;
    private int cornerRadius;
    private int strokeWidth;
    private int strokeColor;
    private boolean isRadiusHalfHeight;
    private boolean isWidthHeightEqual;

    public MsgView(Context context) {
        super(context);
        obtainAttributes(context, null);
    }

    public MsgView(Context context, AttrSet attrs) {
        this(context, attrs, null);
        obtainAttributes(context, attrs);
    }

    public MsgView(Context contexts, AttrSet attrs, String defStyleAttr) {
        super(contexts, attrs, defStyleAttr);
        obtainAttributes(contexts, attrs);
    }

    public MsgView(Context context, Config config) {
        super(context);
        obtainAttributes(context, null);
        if (config != null) {
            if (config.backgroundColor != null) {
                this.backgroundColor = config.backgroundColor;
            }
            if (config.cornerRadius != null) {
                this.cornerRadius = config.cornerRadius;
            }
            if (config.strokeWidth != null) {
                this.strokeWidth = config.strokeWidth;
            }
            if (config.strokeColor != null) {
                this.strokeColor = config.strokeColor;
            }
            if (config.isRadiusHalfHeight != null) {
                this.isRadiusHalfHeight = config.isRadiusHalfHeight;
            }
            if (config.isWidthHeightEqual != null) {
                this.isWidthHeightEqual = config.isWidthHeightEqual;
            }
        }
    }

    public static class Config {
        Integer backgroundColor;
        Integer cornerRadius;
        Integer strokeWidth;
        Integer strokeColor;
        Boolean isRadiusHalfHeight;
        Boolean isWidthHeightEqual;
    }

    private void obtainAttributes(Context context, AttrSet attrs) {
        backgroundColor = AttrUtils.getColorFromAttr(attrs, "mv_backgroundColor", 0xffff5454);
        cornerRadius = AttrUtils.getDimensionFromAttr(attrs, "mv_cornerRadius", 0);
        strokeWidth = AttrUtils.getDimensionFromAttr(attrs, "mv_strokeWidth", 0);
        strokeColor = AttrUtils.getColorFromAttr(attrs, "mv_strokeColor", Color.TRANSPARENT.getValue());
        isRadiusHalfHeight = AttrUtils.getBooleanFromAttr(attrs, "mv_isRadiusHalfHeight", false);
        isWidthHeightEqual = AttrUtils.getBooleanFromAttr(attrs, "mv_isWidthHeightEqual", false);
        setBgSelector();
        invalidate();
    }

    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        setBgSelector();
        invalidate();
    }

    public void setCornerRadius(int cornerRadius) {
        this.cornerRadius = dp2px(cornerRadius);
        setBgSelector();
        invalidate();
    }

    public void setStrokeWidth(int strokeWidth) {
        this.strokeWidth = dp2px(strokeWidth);
        setBgSelector();
        invalidate();
    }

    public void setStrokeColor(int strokeColor) {
        this.strokeColor = strokeColor;
        setBgSelector();
        invalidate();
    }

    public void setIsRadiusHalfHeight(boolean isRadiusHalfHeight) {
        this.isRadiusHalfHeight = isRadiusHalfHeight;
        setBgSelector();
        invalidate();
    }

    public void setIsWidthHeightEqual(boolean isWidthHeightEqual) {
        this.isWidthHeightEqual = isWidthHeightEqual;
        setBgSelector();
        invalidate();
    }

    public int getBackgroundColor() {
        return backgroundColor;
    }

    public int getCornerRadius() {
        return cornerRadius;
    }

    public int getStrokeWidth() {
        return strokeWidth;
    }

    public int getStrokeColor() {
        return strokeColor;
    }

    public boolean isRadiusHalfHeight() {
        return isRadiusHalfHeight;
    }

    public boolean isWidthHeightEqual() {
        return isWidthHeightEqual;
    }

    private int dp2px(float dp) {
        return (int) (mContext.getResourceManager().getDeviceCapability().screenDensity / 160f * dp);
    }

    private int sp2px(float sp) {
        return (int) (mContext.getResourceManager().getDeviceCapability().screenDensity / 160f * sp);
    }

    public void setBgSelector() {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setCornerRadius(40);
        shapeElement.setRgbColor(RgbColor.fromArgbInt(backgroundColor));
        setBackground(shapeElement);
    }
}
